package jbit.action;

import org.apache.struts2.ServletActionContext;

import jbit.util.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import jbit.entity.ClaimVoucher;
import jbit.entity.ClaimVoucherDetail;
import jbit.entity.Employee;
import jbit.service.ClaimVoucherService;

import jbit.constants.Constants;


public class ClaimVoucherAction extends ActionSupport{
	
	private ClaimVoucherService claimVoucherService;
	private ClaimVoucher claimVoucher;
	private ClaimVoucherDetail claimVoucherDetail;
	private Employee employee;
	
	private PageBean pageBean;// 包含分布信息的bean
	private String type;
	private int currentGroup = 1;
	private int page = 1;// 第几页
	
	public String save(){
		System.out.println("save");
		ClaimVoucher claimVoucher1=null;
		ActionContext actionContext=ActionContext.getContext();
		employee=(Employee)actionContext.getSession().get("employee");
		claimVoucher.setSysEmployeeByCreateSn(employee);
		claimVoucher.setSysEmployeeByNextDealSn(employee);
		claimVoucher.setStatus(Constants.CLAIMVOUCHER_SUBMITTED);
		try {
			claimVoucher1=claimVoucherService.saveClaimVoucher(claimVoucher);
			actionContext.getSession().put("claimVoucher", claimVoucher1);
			return SUCCESS;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return INPUT;
		}
	}
	public String  submit() {
		System.out.println("submit");
		ClaimVoucher claimVoucher1=null;
		ActionContext actionContext=ActionContext.getContext();
		employee=(Employee)actionContext.getSession().get("employee");
		claimVoucher.setSysEmployeeByCreateSn(employee);
		claimVoucher.setSysEmployeeByNextDealSn(employee);
		claimVoucher.setStatus(Constants.CLAIMVOUCHER_SUBMITTED);
		try {
			
			claimVoucher1=claimVoucherService.saveClaimVoucher(claimVoucher);
			return SUCCESS;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return INPUT;
		}
	}
	public String addDetail(){
		System.out.println("addDetail");
		ActionContext actionContext=ActionContext.getContext();
		employee=(Employee)actionContext.getSession().get("employee");
		
		//claimVoucher=(ClaimVoucher)actionContext.getSession().get("claimVoucher");
		claimVoucher.setSysEmployeeByCreateSn(employee);
		claimVoucher.setSysEmployeeByNextDealSn(employee);
		System.out.println("claimVid="+claimVoucher.getId());
		System.out.println("claimVoucher="+claimVoucher);
		
		try {
			claimVoucherDetail.setBizClaimVoucher(claimVoucher);
			System.out.println("claimVoucherDetail "+claimVoucherDetail);
			System.out.println("claimVid="+claimVoucher.getId());
			claimVoucher=claimVoucherService.addClaimVocherDetail(claimVoucherDetail);
			actionContext.getSession().put("claimVoucherDetail",claimVoucher.getBizClaimVoucherDetails());
			actionContext.getSession().put("claimVoucher",claimVoucher);
			return SUCCESS;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return INPUT;
		}
		
	}
	public String delDetail(){
		System.out.println("delDetail");
		ActionContext actionContext=ActionContext.getContext();
		employee=(Employee)actionContext.getSession().get("employee");
		claimVoucher.setSysEmployeeByCreateSn(employee);
		claimVoucher.setSysEmployeeByNextDealSn(employee);
		
		String id=ServletActionContext.getRequest().getParameter("id");
		try {
			claimVoucherDetail.setBizClaimVoucher(claimVoucher);
			claimVoucher=claimVoucherService.delClaimVoucherDetail(Integer.parseInt(id));
			actionContext.getSession().remove("claimVoucherDetail");
			actionContext.getSession().put("claimVoucherDetail",claimVoucher.getBizClaimVoucherDetails());
			actionContext.getSession().put("claimVoucher",claimVoucher);
			return SUCCESS;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return INPUT;
		}
		
	}
	
	 // 审核人员（普通员工，部门经理，总经理，财务）进行查询
    public String searchClaimVoucher() {
    	 ActionContext actionContext=ActionContext.getContext();
    	 employee=(Employee)actionContext.getSession().get("employee");
    	 ClaimVoucher newClaimVoucher = new ClaimVoucher();
         if (employee.getSysPosition().getNameCn().equals(Constants.POSITION_FM)) {// 部门经理查询
             newClaimVoucher.setStatus(Constants.CLAIMVOUCHER_CREATED);
             setPageBean1(newClaimVoucher);
         } else if (employee.getSysPosition().getNameCn().equals(Constants.POSITION_GM)
                 || employee.getSysPosition().getNameCn().equals(Constants.POSITION_CASHIER)) {// 总经理和财务查询
             newClaimVoucher.setSysEmployeeByNextDealSn(employee);
             setPageBean1(newClaimVoucher);
         } else if (employee.getSysPosition().getNameCn().equals(Constants.POSITION_STAFF)) {// 员工查询
             newClaimVoucher.setStatus(Constants.CLAIMVOUCHER_CREATED);
             setPageBean2(newClaimVoucher);
         }
         pageBean.setCurrentGroup(currentGroup);
         actionContext.getSession().put("pageBean", pageBean);
         return SUCCESS;
    }
    private void setPageBean1(ClaimVoucher newClaimVoucher){
        if (null != type && type.equals("group")) {
            pageBean = claimVoucherService.queryClaimVoucher(5, currentGroup * 5 - 4, newClaimVoucher, employee);
        } else if (null != type && type.equals("newSearch")) {// 2010/03/23
            // 新增一个按日期查询功能
            newClaimVoucher.setCreateTime(claimVoucher.getCreateTime());
            pageBean = claimVoucherService.queryByDate(5, page, newClaimVoucher, employee);
        } else {
            pageBean = claimVoucherService.queryClaimVoucher(5, page, newClaimVoucher, employee);
        }
    }

    private void setPageBean2(ClaimVoucher newClaimVoucher){
        if (null != type && type.equals("group")) {
            pageBean = claimVoucherService.queryForPage(5, currentGroup * 5 - 4, claimVoucher, employee);
        } else if (null != type && type.equals("newSearch")) {// 2010/03/23
            // 新增一个按日期查询功能            
            newClaimVoucher.setCreateTime(claimVoucher.getCreateTime());
            pageBean = claimVoucherService.queryByDate(5, page, newClaimVoucher, employee);
        } else {
            pageBean = claimVoucherService.queryForPage(5, page, newClaimVoucher, employee);
        }
    }
	public ClaimVoucherService getClaimVoucherService() {
		return claimVoucherService;
	}
	public void setClaimVoucherService(ClaimVoucherService claimVoucherService) {
		this.claimVoucherService = claimVoucherService;
	}
	public ClaimVoucher getClaimVoucher() {
		return claimVoucher;
	}
	public void setClaimVoucher(ClaimVoucher claimVoucher) {
		this.claimVoucher = claimVoucher;
	}
	public ClaimVoucherDetail getClaimVoucherDetail() {
		return claimVoucherDetail;
	}
	public void setClaimVoucherDetail(ClaimVoucherDetail claimVoucherDetail) {
		this.claimVoucherDetail = claimVoucherDetail;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getCurrentGroup() {
		return currentGroup;
	}
	public void setCurrentGroup(int currentGroup) {
		this.currentGroup = currentGroup;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	
	
	
}
